import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../../common/get/controller/xmm_base_controller.dart';
import '../../../../../data/repository/xmm_remote_repository.dart';
import '../../../../../entity/user/xmm_user.dart';
import '../../../../../global/user/xmm_account_manager.dart';
import '../../../../../global/xmm_app_const.dart';
import '../../../../../utils/xmm_event_bus.dart';
import '../../../../../utils/xmm_toast_util.dart';

class XmmFollowController extends XmmBaseGetxController {
  var remoteRepository = XmmRemoteRepository();

  final followList = <XmmUser>[].obs;

  @override
  void onReady() {
    super.onReady();
    XmmEventBus.instance.on(XmmAppConst.eventFollowStatusUpdateKey, (arg) => getFollowList());
  }

  @override
  void onClose() {
    XmmEventBus.instance.off(XmmAppConst.eventFollowStatusUpdateKey);
    super.onClose();
  }

  Future<void> getFollowList() async {
    var list = await remoteRepository.getFollowList();
    followList.value = list;
    followList.refresh();
    XmmAccountManager.instance.updateUserRelation(followList: list);
  }

  Future<void> unfollow(XmmUser user) async {
    try {
      EasyLoading.show();
      await XmmAccountManager.instance.unfollow(user.userId ?? "");
      XmmToastUtils.show("Unfollow Successfully");
    } finally {
      EasyLoading.dismiss();
    }
  }
}
